{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "###############################\n",
    "## 分类信息合并\n",
    "###############################\n",
    "import json\n",
    "\n",
    "# We have two dictionaries to combine\n",
    "# json_obj_1 = {\"a\": [\"1234\",2], \"b\":[2,3], 'c': [1,2,3]}\n",
    "# json_obj_2 = {\"a\": [\"5678\",4], 'd':[4,2], 'e': [4,2,2]}\n",
    "with open('weapon.json') as f1:\n",
    "    json_obj_1 = json.load(f1)\n",
    "with open('cat.json') as f2:\n",
    "    json_obj_2 = json.load(f2)\n",
    "hold_json_obj = {}\n",
    "# We'll loop through every item in the json_obj_1 dictionary\n",
    "for item_1 in json_obj_1:\n",
    "    # We'll also loop through every item in the json_obj_2 dictionary\n",
    "    for item_2 in json_obj_2:\n",
    "        # Now let's compare whether they are the same KEYS (not values)\n",
    "        if item_1 == item_2:\n",
    "            # if they match, we create a list to store the array\n",
    "            hold_array = []\n",
    "            hold_array.extend(json_obj_1[item_1])\n",
    "            hold_array.extend(json_obj_2[item_1])\n",
    "            # finally putting the array to our hold_json_obj\n",
    "            hold_json_obj[item_1] = hold_array\n",
    "        else:\n",
    "            # if they don't match, check if the key already exists in the\n",
    "            # hold_json_obj because we might be iterating json_obj_2 for the second time.\n",
    "            if item_2 not in hold_json_obj:\n",
    "                #add the ummatched array to hold_json_obj\n",
    "                hold_json_obj[item_2] = json_obj_2[item_2]\n",
    "                \n",
    "json_obj_1.update(hold_json_obj)\n",
    "json = json.dumps(json_obj_1)\n",
    "print(str(json))\n",
    "with open('cat.json', 'w') as f:\n",
    "    f.write(str(json))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "12743\n"
     ]
    }
   ],
   "source": [
    "import json\n",
    "\n",
    "with open(\"cat.json\") as f:\n",
    "    j = json.load(f)\n",
    "    print(len(j))"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "7b4b4feff2f24a0f0a34464dbe537a36fda679851528fb8735cb41fa49dffb2d"
  },
  "kernelspec": {
   "display_name": "Python 3.8.3 ('base')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
